package com.applet.controller.common;

import com.applet.common.constant.ResultConstant;
import com.applet.common.properties.FileProperties;
import com.applet.common.utils.AliOSSUtils;
import com.applet.common.utils.FileUtil;
import com.applet.common.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author “XuShao”
 * @date 2024/12/16 21:46
 * @description
 */
@RestController
@Tag(name = "公共接口")
@Slf4j
public class UploadController {

    // TODO: 2024/12/21 未完善 
   /* @Value("${zyz.file.image-path}")
    private String imagePath;

    @Value("${zyz.file.video-path}")
    private String videoPath;

    @Autowired
    private FileUtil fileUtil;

    @Operation(summary = "图片上传")
    @PostMapping(value = "/admin/common/uploadImage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<String> unloadImage(@RequestPart("file") MultipartFile file) {
        log.info("图片上传 => {}", file.getOriginalFilename());
        if(!fileUtil.validateImage(file)) return Result.fail(ResultConstant.FAIL_FILL_UPLOAD_VALIDATE_FAIL.getCode(), ResultConstant.FAIL_FILL_UPLOAD_VALIDATE_FAIL.getMessage());
        return getStringResult(file, imagePath);
    }

    @Operation(summary = "视频上传")
    @PostMapping(value = "/admin/common/uploadVideo", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<String> unloadVideo(@RequestPart("file") MultipartFile file) {
        log.info("视频上传 => {}", file.getOriginalFilename());
        if(!fileUtil.validateVideo(file)) return Result.fail(ResultConstant.FAIL_FILL_UPLOAD_VALIDATE_FAIL.getCode(), ResultConstant.FAIL_FILL_UPLOAD_VALIDATE_FAIL.getMessage());
        return getStringResult(file, videoPath);
    }

    private Result<String> getStringResult(MultipartFile file, String path) {
        File save = new File(path);
        if(!save.exists()) save.mkdirs();
        String filePath =  UUID.randomUUID() + fileUtil.getFileSuffix(file.getOriginalFilename());
        try {
            file.transferTo(new File(save.getAbsolutePath() + "/" + filePath));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return Result.success("http://localhost:8080/images/" + filePath);
    }*/

    //上传到阿里云OSS
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Autowired
    private FileUtil fileUtil;

    @Autowired
    private FileProperties fileProperties;

    @Operation(summary = "视频上传")
    @PostMapping(path = "/admin/uploadVideo", headers = "content-type=multipart/form-data")
    public Result<?> uploadVideo(MultipartFile file) throws IOException {
        log.info("视频上传 => {}", file.getOriginalFilename());
        if(!fileUtil.validate(file, fileProperties.getVideoSize(), fileProperties.getVideoSuffix()))
            return Result.fail(ResultConstant.FAIL_FILL_UPLOAD_VALIDATE_FAIL.getCode(), ResultConstant.FAIL_FILL_UPLOAD_VALIDATE_FAIL.getMessage());
        Object fileUrl = aliOSSUtils.upload(file);
        log.info("上传文件路径为 => {}", fileUrl);
        return Result.success(fileUrl);
    }

    @Operation(summary = "图片上传")
    @PostMapping(path = "/admin/uploadImg", headers = "content-type=multipart/form-data")
    public Result<?> uploadImg(@RequestPart MultipartFile file) throws Exception {
        log.info("图片上传 => {}", file.getOriginalFilename());
        if(!fileUtil.validate(file, fileProperties.getImageSize(), fileProperties.getImageSuffix())) return Result.fail(ResultConstant.FAIL_FILL_UPLOAD_VALIDATE_FAIL.getCode(), ResultConstant.FAIL_FILL_UPLOAD_VALIDATE_FAIL.getMessage());
        Object fileUrl = aliOSSUtils.upload(file);
        log.info("上传文件路径为 => {}", fileUrl);
        return Result.success(fileUrl);
    }

    @Operation(summary = "作品上传")
    @PostMapping(path = "/user/uploadCreation", headers = "content-type=multipart/form-data")
    public Result<?> uploadCreation(MultipartFile file) throws Exception {
        log.info("作品上传 => {}", file.getOriginalFilename());
        if(!fileUtil.validate(file, fileProperties.getCreationSize(), fileProperties.getCreationSuffix())) return Result.fail(ResultConstant.FAIL_FILL_UPLOAD_VALIDATE_FAIL.getCode(), ResultConstant.FAIL_FILL_UPLOAD_VALIDATE_FAIL.getMessage());
        Object fileUrl = aliOSSUtils.upload(file);
        log.info("上传文件路径为 => {}", fileUrl);
        return Result.success(fileUrl);
    }
}
